package com.chanyachting.emall.action.register;

import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.MultipartRequestHandler;

import com.chanyachting.emall.domain.Company;
import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.exception.AccountExistException;
import com.chanyachting.emall.exception.EmailExistException;
import com.chanyachting.emall.form.MemberLoginForm;
import com.chanyachting.emall.service.CompanyService;
import com.chanyachting.emall.service.MemberService;
import com.chanyachting.emall.serviceImpl.CompanyServiceImpl;
import com.chanyachting.emall.serviceImpl.MemberServiceImpl;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.RequestUtil;

public class MemberRegisterAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RequestUtil ru=new RequestUtil(request);
		
		MemberService mService = new MemberServiceImpl();
		CompanyService cService = new CompanyServiceImpl();
		MemberLoginForm naForm=(MemberLoginForm)form;
		
		Member m = new Member();
		Company c = new Company();
		MultipartRequestHandler mm = form.getMultipartRequestHandler();
		Hashtable h = mm.getTextElements();
		
		Iterator iter = h.entrySet().iterator();
		
		while(iter.hasNext()) {
			Map.Entry entry  = (Map.Entry)iter.next();
			String val[] = (String[]) entry.getValue();
			System.out.println("key=" + entry.getKey() + ";value=" + val[0]);
		}
		m.setAccount(naForm.getEmail());
		m.setArea(naForm.getArea());
		m.setBackupEmail(naForm.getBackupEmail());
//		m.setBeginDate(_beginDate)
		m.setBlog(naForm.getBlog());
		m.setEmail(naForm.getEmail());
//		m.setEndDate(_endDate);
//		m.setImgUrl(_imgUrl);
		m.setJob(naForm.getJob());
		m.setJobType(naForm.getJobType());
		m.setMemberBirthday(naForm.getMemberBirthday());
		m.setMemberName(naForm.getMemberName());
		m.setMemberType("00");
		m.setPassword(naForm.getPassword());
		m.setSex(naForm.getSex());
		m.setWebContact(naForm.getWebContact());
		
		m.setLoginIP(request.getRemoteHost());
		m.setRegistIP(request.getRemoteHost());
		m.setLoginDate(DateUtil.convertDateTime2String(new Date()));
		m.setRegistDate(DateUtil.convertDateTime2String(new Date()));
//		m.setId(10);
		try {
			if (naForm.getName() != null && !"".equals(naForm.getName())) {
				m.setMemberType("01");
				
				c.setTurnover(naForm.getTurnover());
				c.setLinkManEmail(naForm.getLinkManEmail());
				c.setMainServiceArea(naForm.getMainServiceArea());
				c.setLinkManJob(naForm.getLinkManJob());
				c.setSecondAddress(naForm.getSecondAddress());
				c.setFax(naForm.getFax());
				c.setStreetName(naForm.getStreetName());
				c.setLinkManPhone(naForm.getLinkManPhone());
				c.setFirstAddress(naForm.getFirstAddress());
				c.setYellowPageImgUrl(naForm.getYellowPageImgUrl());
				c.setWebPage(naForm.getWebPage());
				c.setPostcode(naForm.getPostcode());
				c.setLinkManName(naForm.getLinkManName());
				c.setTel(naForm.getTel());
				c.setName(naForm.getName());
				c.setBusinessTypeId(naForm.getBusinessTypeId());
				c.setBusinessCategoryId(naForm.getBusinessCategoryId());
				c.setEmployeeNumber(naForm.getEmployeeNumber());
				c.setCompanySummary(naForm.getCompanySummary());
				c.setLogoUrl2(naForm.getLogoUrl2());
				c.setYellowPageSummary(naForm.getYellowPageSummary());
				c.setLinkManWeb(naForm.getLinkManWeb());
				c.setCompanyType(naForm.getCompanyType());
				c.setMainServiceProduct(naForm.getMainServiceProduct());
				c.setDetailAddress(naForm.getDetailAddress());
				c.setCompanyState(naForm.getCompanyState());
				c.setCompanyCreateDate(naForm.getCompanyCreateDate());
				c.setLogoUrl(naForm.getLogoUrl());
				c.setCoordinate(naForm.getCoordinate());
				c.setPri(naForm.getPri());
				c.setCreateDate(DateUtil.convertDateTime2String(new Date()));
				c.setImportTurnover(naForm.getImportTurnover());
			}
			mService.registMember(m);
			
		} catch (AccountExistException e) {
			ru.setMsg("用户名已存在！");
			return mapping.findForward("error");
		} catch (EmailExistException e) {
			ru.setMsg("注册的邮箱已存在！");
			return mapping.findForward("error");
		} catch (Exception e) {
			ru.setMsg("出现未知错误，请联系管理员！");
			return mapping.findForward("error");
		}
		
		if (naForm.getName() != null && !"".equals(naForm.getName())) {
			cService.createCompany(c, m);
		}
		return mapping.findForward("registSuccess");
	}
}
